package com.ctcc.util;
import com.ctcc.serviceImpl.DepartmentImpl;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.Key;
import java.util.UUID;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.*;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.core.io.Resource;
/**
 * Created by Peanut on 2018/1/29.
 */
@Service
public class HttpUtil {
    Log log= LogFactory.getLog(HttpUtil.class);
    @Value("${cloudhub.key}")
    public  Resource keyFile;

    public  Object  org_yunzhijia(String url,JsonNode jsonData ) throws Exception {
        HttpResponse<JsonNode> jsonResponse;

        try {
            byte[] keyByte = Files.readAllBytes(Paths.get(keyFile.getURI()));
            /*byte[] keyByte = EncryptUtils.getBytesFromFile(keyFile);*/
            Key key = EncryptUtils.restorePrivateKey(keyByte);
            jsonResponse = Unirest
                    .post(url)
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .field("eid", "12553048")
                    .field("nonce", UUID.randomUUID().toString())
                    .field("data", EncryptUtils.encryptWithEncodeBase64UTF8(
                            jsonData.toString(), key)).asJson();
            log.info("请求云之家接口成功，接口地址：" +url);
            return jsonResponse.getBody();


        } catch (UnirestException e) {
            e.printStackTrace();
            jsonResponse=null;
        }
        return jsonResponse;
    }
    public  Object  org_yunzhijia_form(String url,JsonNode jsonData ) throws Exception {
        DefaultHttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(url);
                String result  = null;
                try {
                       StringEntity stringEntity = new StringEntity(jsonData.toString());
                        stringEntity.setContentEncoding("UTF-8");
                        stringEntity.setContentType("application/json");//发送json数据需要设置contentType
                        post.setEntity(stringEntity);
                        org.apache.http.HttpResponse res = client.execute(post);
                        if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                                HttpEntity entity = res.getEntity();
                                result = EntityUtils.toString(res.getEntity());// 返回json格式：

                         }
                   } catch (Exception e) {
                       throw new RuntimeException(e);
                  }
                return result;
    }
}
